<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-14
 * Time: 21:36
 */

namespace app\api\service\partner;


use app\api\model\Assistant;
use app\api\model\AssistantMoney;
use app\api\model\CityPartner;
use app\api\model\CityPartnerMoney;
use app\api\model\CountyPartner;
use app\api\model\CountyPartnerMoney;
use app\api\model\ProvincePartner;
use app\api\model\ProvincePartnerMoney;
use app\api\model\User;
use app\api\service\Token;

class PartnerSer extends Token
{
    // 类型
    private $status;

    // 用户信息
    private $user;

    // 如果是列表 需要有分页
    private $page;

    // 一页多少个
    private $limit;


    // 信息
    private $partner;

    function __construct($status,$page,$limit)
    {
        $this->status = $status;
        $this->page = $page;
        $this->limit = $limit;
    }

    public function get()
    {
        self::judgeMember();
        $uid = self::getCurrentUid();
        $this->user = User::get($uid);
        if($this->user['partner_status'] == 1) {
            $this->partner = ProvincePartner::where('user_id',$uid)->find();
        } elseif($this->user['partner_status'] == 2) {
            $this->partner = CityPartner::where('user_id',$uid)->find();
        } elseif($this->user['partner_status'] == 3) {
            $this->partner = CountyPartner::where('user_id',$uid)->find();
        } elseif($this->user['partner_status'] == 4) {
            $this->partner = Assistant::where('user_id',$uid)->find();
        }
        if($this->status == 1) {
            return $this->partner;
        } else {
            return $this->getPartnerMoneyList();
        }
    }

    // 获取
    private function getPartnerMoneyList()
    {
        if($this->user['partner_status'] == 1) {
            $query = ProvincePartnerMoney::with('user.member')->where('province_partner_id',$this->partner['id'])->order('id desc');
        } elseif($this->user['partner_status'] == 2) {
            $query = CityPartnerMoney::with('user.member')->where('city_partner_id',$this->partner['id'])->order('id desc');
        } elseif($this->user['partner_status'] == 3) {
            $query = CountyPartnerMoney::with('user.member')->where('county_partner_id',$this->partner['id'])->order('id desc');
        } elseif($this->user['partner_status'] == 4) {
            $query = AssistantMoney::with('user.member')->where('assistant_id',$this->partner['id'])->order('id desc');
        }
        $count = $query->count();
        $result = $query->page($this->page, $this->limit)->select();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }
}